
#include <BStyle>
#include <BIcon>
#include <BPainter>
#include <BMouseEvent>

#include "PixmapArea.h"
#include "ColorPicker.h"

ColorPicker::ColorPicker() : Operation("Color Picker")
{
	_cursor = new BCursor();
}
ColorPicker::~ColorPicker()
{

}

void ColorPicker::attached(BWidget* widget)
{
	if (const BStyle* style = widget->realStyle())
	{
		if (BIcon* icon = style->icon("color-pick"))
		{
			_cursor->setImage(icon->image(State_Normal));
			_cursor->setPoint(4, 22);
		}
		widget->setCursor(_cursor);
	}
	Operation::attached(widget);
}
void ColorPicker::detached(BWidget* widget)
{
	widget->setCursor(0);
	Operation::detached(widget);
}

void ColorPicker::paint(BPainter& painter)
{
	PixmapArea* pixmapArea = this->pixmapArea();
	if (_pressed)
	{
		painter.setColor(255, 0, 0);
		painter.drawRect(_pickRect);

		BRect rect = pixmapArea->mapToPixmap(_pickRect);
		rect.left() -= 3;
		rect.right() += 3;
		rect.top() -= 3;
		rect.bottom() += 3;
		rect = pixmapArea->mapFromPixmap(rect);
		painter.setColor(55, 0, 220);
		painter.drawRect(rect);
	}
}

void ColorPicker::freshEvent(const BEvent& event)
{

}

bool ColorPicker::focusEvent(const BEvent& event)
{
	return false;
}
bool ColorPicker::keyDownEvent(const BKeyEvent& keyEvent)
{
	return false;
}
bool ColorPicker::keyUpEvent(const BKeyEvent& keyEvent)
{
	return false;
}

bool ColorPicker::mouseEnterEvent(const BMouseEvent& mouseEvent)
{
	return false;
}
bool ColorPicker::mouseLeaveEvent(const BMouseEvent& mouseEvent)
{
	return false;
}
bool ColorPicker::mousePressEvent(const BMouseEvent& mouseEvent)
{
	if (mouseEvent.button() == Button_Left)
	{
		_pressed = true;
		_pressPoint = mouseEvent.pos();
		return true;
	}
	return false;
}
bool ColorPicker::mouseReleaseEvent(const BMouseEvent& mouseEvent)
{
	if (mouseEvent.button() == Button_Left)
	{
		emit("pick-finished", _pickColor);
		_pressed = false;
		return true;
	}
	return false;
}
bool ColorPicker::mouseMoveEvent(const BMouseEvent& mouseEvent)
{
	PixmapArea* pixmapArea = this->pixmapArea();
	BImage* image = pixmapArea->image();
	if (mouseEvent.button() == Button_Left)
	{
		BPoint point = mouseEvent.pos();
		_pickRect.reset(_point);
		int hwidth = abs(point.x() - _pressPoint.x());
		int hheight = abs(point.y() - _pressPoint.y());
		_pickRect.left() -= hwidth;
		_pickRect.right() += hwidth;
		_pickRect.top() -= hheight;
		_pickRect.bottom() += hheight;

		BUInt count = _pickRect.width() * _pickRect.height();
		if (count > 0)
		{
			BUInt red = 0;
			BUInt green = 0;
			BUInt blue = 0;
			BUInt alpha = 0;
			BRect rect = pixmapArea->mapToPixmap(_pickRect);
			for (int y = rect.top(); y < rect.bottom(); y++)
			{
				for (int x = rect.left(); x < rect.right(); x++)
				{
					BColor color = image->pixelColor(x, y);
					red += color.r();
					green += color.g();
					blue += color.b();
					alpha += color.a();
				}
			}
			red /= count;
			green /= count;
			blue /= count;
			alpha /= count;
			_pickColor.set(red, green, blue, alpha);
		}
	}
	else
	{
		_point = mouseEvent.pos();
		BPoint point = pixmapArea->mapToPixmap(_point);
		_pickColor = image->pixelColor(point.x(), point.y());
	}
	emit("pick-color", _pickColor);
	if (BObject* object = this->asset())
		object->setAsset(_pickColor);
	pixmapArea->fresh();
	return false;
}
